#!/usr/bin/ruby -w

require 'rubygems'
require 'rake/gempackagetask'
require 'rake/clean'
require 'rake/rdoctask'
require 'rake/testtask'


#Get locations of required compilers.
RUBYSCRIPT2EXE = ENV['RUBYSCRIPT2EXE'] || File.join(Config::CONFIG['bindir'], 'rubyscript2exe')
MAKENSIS = ENV['MAKENSIS'] || File.join(ENV['ProgramFiles'], "nsis", "makensis.exe")

#Get other variables.
RELEASE_VERSION = ENV['ITCH_VERSION'] or raise "ITCH_VERSION environment variable not defined."
AUTHOR = "Jay McGavren"
AUTHOR_EMAIL = "chicgeekaz@hotmail.com"
WEB_SITE = "http://code.google.com/p/itunes-control/"

#Set up rdoc.
RDOC_OPTIONS = [
	'--title', 'Itch -- Control Helper for iTunes',
	'--main', 'README.txt'
]

#Get file names.
EXECUTABLE = "bin/itch"
WINDOWS_EXECUTABLE = "build/itch.exe"
BATCH_FILES_GENERATOR = "nsis/make_batch_files"
BATCH_FILES_DIRECTORY = "build/batch_files"
WINDOWS_INSTALLER = "pkg/itch-setup-#{RELEASE_VERSION}.exe"
NSIS_SCRIPT = "nsis/itch.nsi"


desc "Set up directories"
task :setup => ["build", "pkg"]

desc "Create an executable"
task :compile => :setup
task :compile => WINDOWS_EXECUTABLE

desc "Create a distributable"
task :distribute => :compile
task :distribute => :make_batch_files
task :distribute => WINDOWS_INSTALLER

desc "Create a gem and a Windows installer by default"
task :default => [:test, :distribute, :gem]


directory "build"
directory BATCH_FILES_DIRECTORY
directory "pkg"


desc "Compile a Windows executable"
file WINDOWS_EXECUTABLE => [EXECUTABLE] do |target|
	#Ensure rubyscript2exe is installed.
	unless File.exist?(RUBYSCRIPT2EXE)
		raise "Cannot find rubyscript2exe.  Set RUBYSCRIPT2EXE environment variable to full path of rubyscript2exe, and run again."
	end
	#Compile an executable.
	system %{ruby -I lib #{RUBYSCRIPT2EXE} #{target.prerequisites.join(' ')}}
	#Move executable to target location, as it is always placed in current directory.
	mv 'itch.exe', target.name
end


desc "Create a directory with sample batch files"
task :make_batch_files => [BATCH_FILES_DIRECTORY, BATCH_FILES_GENERATOR] do
	system %{ruby #{BATCH_FILES_GENERATOR} #{BATCH_FILES_DIRECTORY}}
end


desc "Create a NSIS installer"
file WINDOWS_INSTALLER => [WINDOWS_EXECUTABLE, NSIS_SCRIPT, :make_batch_files] do |target|
	#Ensure NSIS is installed.
	unless File.exist?(MAKENSIS)
		raise "Cannot find makensis.  Set MAKENSIS environment variable to full path of makensis, and run again."
	end
	#Run the installer creation script.
	system [
		MAKENSIS,
		"/V2", #Surpress info messages.
		"/Obuild/nsis.log", #Log compiler output instead of outputting to STDOUT.
		"/NOCD", #Do not CD to NSI script directory before running.
		"/DPRODUCT_VERSION=#{RELEASE_VERSION}", #Define in-NSI-script variables.
		"/DPRODUCT_PUBLISHER=#{AUTHOR}",
		"/DPRODUCT_WEB_SITE=#{WEB_SITE}",
		"/XOutFile #{target.name}",
		NSIS_SCRIPT #Script to run.
	].map{|v| %("#{v}")}.join(' ')
end


desc "Create documentation"
Rake::RDocTask.new do |rdoc|
	rdoc.rdoc_dir = 'doc'
	rdoc.rdoc_files = FileList[
		'lib/**/*',
		'*.txt'
	].exclude(/\bsvn\b/).to_a
	rdoc.options = RDOC_OPTIONS
end


desc "Test the package"
Rake::TestTask.new do |test|
	test.libs << 'lib'
	test.test_files = FileList['test/test_*.rb']
end


desc "Package a gem"
specification = Gem::Specification.new do |spec|
	spec.name = 'itch'
	spec.version = RELEASE_VERSION
	spec.author = AUTHOR
	spec.email = AUTHOR_EMAIL
	spec.homepage = WEB_SITE
	spec.platform = Gem::Platform::RUBY
	spec.summary = "Allows control of iTunes for Windows via a command line."
	spec.requirements << 'iTunes for Windows'
	spec.rubyforge_project = 'itch'
	spec.require_path = 'lib'
	spec.autorequire = 'itch'
	spec.test_files = Dir.glob('test/test_*.rb')
	spec.has_rdoc = true
	spec.rdoc_options = RDOC_OPTIONS
	spec.extra_rdoc_files = ['README.txt', 'LICENSE.txt']
	spec.files = FileList[
		'*.txt',
		'bin/**/*',
		'lib/**/*',
		'test/**/*',
		'doc/**/*'
	].exclude(/\bsvn\b/).to_a
	spec.executables << 'itch'
end
Rake::GemPackageTask.new(specification) do |package|
	package.need_tar = true
end


CLEAN.include("build")
CLOBBER.include("pkg")
